clear
k = imfinfo('sign2.jpg');
A = imread('sign2.jpg');
if(strcmp(k.ColorType,'truecolor')==1)
    B = rgb2gray(A);
elseif(strcmp(k.ColorType,'grayscale')==1)
    B = A;
end

% figure,imshow(B);

new_dim = max(size(B));

C = zeros(new_dim,new_dim);
C(1:size(B,1),1:size(B,2))=B;

%figure,imshow(mat2gray(C));

alpha1 = 0.30;

T = dFRT(new_dim,alpha1);

F1 = T*C*T;

% if(alpha1 > 0.5)
%     figure,imshow(fftshift(im2uint8(mat2gray(log(1+abs(F1))))));
% else
%     figure,imshow(im2uint8(mat2gray(log(1+abs(F1)))));
% end


alpha2 = 0.85;
X = imread('cameraman.tif');

% figure,imshow(X);

T = dFRT(256,alpha2);

F2 = T*double(X)*T;



[mag_array loc] = sort(abs(F2(:)),'descend');

percent = .4;

start_pt = 1+floor(percent * length(loc));



r = real(F2(loc(start_pt:start_pt+length(F1(:))-1)));
im = imag(F2(loc(start_pt:start_pt+length(F1(:))-1)));


add_r = real(F1(:));
add_im = imag(F1(:));

max_real = max(add_r);
max_imag = max(add_im);

% plot(add_r);
% hold on;
%plot(add_r(1:10)/max_real,'r--o');

factor = 80;

r1 = r + factor*(add_r/max_real);
im1 = im + factor*(add_im/max_imag);

%r1 = r + (add_r);
%im1 = im + (add_im);

% r1 = r;
% im1 = im;

%plot(r1);

F2(loc(start_pt:start_pt+numel(F1)-1)) = r1 + j*im1;

T = dFRT(256,-alpha2);

F3 = T*F2*T;

F3 = im2uint8(mat2gray(abs(F3)));

subplot(1,2,1),subimage(X),title('ORIGINAL IMAGE');

subplot(1,2,2),subimage(mat2gray(abs(F3))),title('WATERMARKED IMAGE');

figure,imshow(im2uint8(mat2gray(abs(F3))));

%imagermse(im2uint8(mat2gray(abs(F3))),X)

imwrite(im2uint8(mat2gray(abs(F3))),'watermarked.tif');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

subplot(4,4,2:3),plot(abs(F1(:))),title('Original Coefficients for Signature');

%for p = 0.0 : 0.05 : 1.0

    T = dFRT(256,alpha2);

% F4 = T*double(im2uint8(mat2gray(abs(F3))))*T;
% H = fspecial('gaussian');
% F3 = imadd(F3,imfilter(F3,H,'conv'));

%F3 = imnoise(F3,'salt & pepper',0.03);

ctr = 1;

plotloc = 5;

row=2;
col=1;

for p = 0:10:100

 F3(1:p,1:p)=0;
% F3(127:256,127:256)=0;

%figure,imshow(F3);

    F4 = T*double(F3)*T;

% figure,imshow(mat2gray(log(1+abs(F2))));
% figure,imshow(mat2gray(log(1+abs(F4))));

    arr_orig = F2(loc(start_pt:start_pt+numel(F1)-1));
    arr_now = F4(loc(start_pt:start_pt+numel(F1)-1));


% plot(1:length(arr_orig),abs(arr_now),'b');
% hold on,plot(abs(arr_orig),'r');

    diff_coeff = arr_now-arr_orig;

    diff_coeff_real = real(diff_coeff)*max_real/factor;

%plot(diff_coeff_real);
    diff_coeff_imag = imag(diff_coeff)*max_imag/factor;

    diff_coeff_final = diff_coeff_real + i*diff_coeff_imag;

    subplot(4,4,plotloc),plot(abs(diff_coeff_final));
    %subplot(1,2,2),plot(abs(F1(:))),title('Original Coefficients for Signature');

    plotloc = plotloc + 1;
    if(plotloc == 5)
        col = 1;
        row = row + 1;
    end
    %row = row + 1;
    
    %col = col + 1;
    
    R = reshape(diff_coeff_final,size(C));

%R = reshape(diff_coeff,size(C));

%ctr = 1;

%for i = 0.0 : 0.005 : 1.0
    H = dFRT(new_dim,-alpha1);
    Y = H*R*H;

    %figure,imshow(im2uint8(mat2gray(abs(Y))));
    
    Z = im2uint8(mat2gray(abs(Y)));
    
    rmse_comp(ctr) = imagermse(Z,C);
    ctr = ctr + 1;
end

ind_axis = 10: 10 : 100;
figure,stem(ind_axis,rmse_comp);
title('RMSE for retrieved image for different values of \alpha_{1}');
text(0.05,6500,'Original Image encoded with \alpha_{1} = 0.30');
xlabel('\alpha_{1} ');
ylabel('RMSE');
%text(7000,0.2,'Encoding value of alpha2 = 0.85');







